/*
	FileName : app_led.c
	Description : 
	
	Created on 2013.10.27 by ZZP.
	
	Revision History:
	
*/

#include "bsp.h"
#include "app.h"
#include "app_led.h"


void WorkLed(void);
void NETLed(void);
void COMLed(void);
void PWRLed(void);

extern uint32_t Toread;

/////////////////////////////////////////////////////////////////////////////////
///							Global Function realizations
/////////////////////////////////////////////////////////////////////////////////
void AppTaskLED(void *p_arg)
{
	(void)p_arg;

	AppInfo.PWRLedTm = 3000;
	
	while (DEF_TRUE){
		
		if (AppInfo.RdyFlag == 1){
			
			WorkLed();
			NETLed();
			COMLed();
			PWRLed();
		}
		
		AppInfo.TaskLedCnt++;
		OSTimeDly(10);
	}
}

/*
 *  Call in task: AppTaskStart()
 */
void LedHandler(void)
{
	AppInfo.WRKLedTm++;
	//AppInfo.NETLedTm++;
	//AppInfo.COMLedTm++;
	AppInfo.PWRLedTm++;
}



/////////////////////////////////////////////////////////////////////////////////
///							Local Function realizations
/////////////////////////////////////////////////////////////////////////////////

/*
 * 
 */
void PWRLed(void)
{
	if(Toread)
	{
		if ((AppInfo.PWRLedTm % 20)   == 0)
		{
			BSP_LED_Off(LED_PWR);
			AppInfo.PWRLedTm = 0;
		}
		else if (AppInfo.PWRLedTm == 10)
		{
			BSP_LED_On(LED_PWR);
		}
		return;
	}
	else
	{
		if (AppInfo.PWRLedTm < 3000)
		{
			BSP_LED_On(LED_PWR);
		}
		else if (AppInfo.PWRLedTm > 3100)
		{
			BSP_LED_On(LED_PWR);
			AppInfo.PWRLedTm = 3000;
		}
		else if (AppInfo.PWRLedTm == 3020 || AppInfo.PWRLedTm == 3012)
		{
			BSP_LED_On(LED_PWR);
		}
		else if (AppInfo.PWRLedTm == 3023 || AppInfo.PWRLedTm == 3017 || AppInfo.PWRLedTm == 3007)
		{
			BSP_LED_Off(LED_PWR);
		}
	}
	
}



/*
 * 
 */
void NETLed(void)
{
	///LED_NET
	if (AppInfo.NetRxFlag)
	{	
		AppInfo.PWRLedTm = 0;
		if (AppInfo.NETLedTm >= 16)
		{	
			BSP_LED_Off(LED_NET);
			
			AppInfo.NETLedTm = 0;
			
			AppInfo.NetRxFlag--;
		}
		else
		{	
			if ((AppInfo.NETLedTm % 8) == 0)
			{
				BSP_LED_On(LED_NET);
			}
			else if ((AppInfo.NETLedTm % 4) == 0)
			{
				 BSP_LED_Off(LED_NET);
			}
			AppInfo.NETLedTm++;
		}
	}
	else
	{
		BSP_LED_Off(LED_NET);
		AppInfo.NETLedTm = 0;
	}
}


/*
 * 
 */
void COMLed(void)
{	
	///LED_COM
	if (AppInfo.UsartRxFlag[COM1])
	{	
		AppInfo.PWRLedTm = 0;
		if (AppInfo.COMLedTm >= 16)
		{	
			BSP_LED_Off(LED_COM);
			AppInfo.COMLedTm = 0;
			AppInfo.UsartRxFlag[COM1]--;
		}
		else
		{	
			if ((AppInfo.COMLedTm % 8) == 0)
			{
				BSP_LED_On(LED_COM);
			}
			else if ((AppInfo.COMLedTm % 4) == 0)
			{
			    BSP_LED_Off(LED_COM);
			}
			AppInfo.COMLedTm++;
		}
	}
	else
	{
		BSP_LED_Off(LED_COM);
		AppInfo.COMLedTm = 0;
	}
}


void WorkLed(void)
{
	//LED_WORK => off : on = 0.618 : 0.382
    if ((AppInfo.WRKLedTm % 100)   == 0)
    {
        BSP_LED_Off(LED_WRK);
		AppInfo.WRKLedTm = 0;
    }
	else if (AppInfo.WRKLedTm == 23)
   	{
   		BSP_LED_On(LED_WRK);
	}
    else if (AppInfo.WRKLedTm == 20)
   	{
   		BSP_LED_Off(LED_WRK);
	}
    else if (AppInfo.WRKLedTm == 17)
   	{
   		BSP_LED_On(LED_WRK);
	}
    else if (AppInfo.WRKLedTm == 12)
   	{
   		BSP_LED_Off(LED_WRK);
	}
    else if (AppInfo.WRKLedTm == 7)
   	{
   		BSP_LED_On(LED_WRK);
	}
}








